package com.atguigu.yygh.user.service.impl;

import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.mapper.PatientMapper;
import com.atguigu.yygh.user.service.PatientService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @description: TODO
 * @author: 化一帆
 * @date: 2022/12/6 18:00
 * @version: v1.0
 */

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    DictFeignClient dictFeignClient;


   /**
    * @description: 根据当前用户的UserID查询当前用户的所有的就诊人
    * @author 化一帆
    * @date 2022/12/6 19:06
    */
    @Override
    public List<Patient> findListByUserId(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);

        patientList.forEach(patient -> {
            this.packPatient(patient);
        });
        return  patientList;
    }




    /**
     * @description:  调用字典微服务，通过value查询对应的String值,将就诊人的信息显示完整
     * @author 化一帆
     * @date 2022/12/6 19:13
     */
    private void packPatient(Patient patient) {
        String certificatesType = patient.getCertificatesType();//根据value查询就诊人证件类型的名称
        String contactsCertificatesType = patient.getContactsCertificatesType();//根据value查询联系人的证件类型名称

        String provinceCode = patient.getProvinceCode();
        String cityCode = patient.getCityCode();
        String districtCode = patient.getDistrictCode();

        //注意：证件类型，省市区   ， 他们的value是唯一的。

//            dictFeignClient.getName();

        String certificatesTypeString = dictFeignClient.getName(certificatesType);
        // /a/b/c/{id}
        // /a/b/c/ == 404
        //如果没有联系人的信息。得不到属性就报错404
        String contactsCertificatesTypeString = "";
        if(!StringUtils.isEmpty(contactsCertificatesType)){
            contactsCertificatesTypeString = dictFeignClient.getName(contactsCertificatesType);
        }
        //其他的也可以判断

        String provinceString = dictFeignClient.getName(provinceCode);
        String cityString = dictFeignClient.getName(cityCode);
        String districtString = dictFeignClient.getName(districtCode);

        String fullAddress = provinceString + cityString + districtString + patient.getAddress();

        patient.getParam().put("certificatesTypeString",certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString",contactsCertificatesTypeString);

        patient.getParam().put("provinceString",provinceString);
        patient.getParam().put("cityString",cityString);
        patient.getParam().put("districtString",districtString);

        patient.getParam().put("fullAddress",fullAddress);
    }


    /**
     * @description:  查询某一个就诊人
     * @author 化一帆
     * @date 2022/12/6 19:21
     */
    @Override
    public Patient getPatient(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packPatient(patient);
        return patient;
    }
}
